﻿using SpecialEffects;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GlobalGameClasses;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Audio;
using Sirian.DrawableObject.Actors;


namespace SirianTestEnvironment
{
    
    
    /// <summary>
    ///Dies ist eine Testklasse für "SpecialEffectTest" und soll
    ///alle SpecialEffectTest Komponententests enthalten.
    ///</summary>
    [TestClass()]
    public class SpecialEffectTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Ruft den Testkontext auf, der Informationen
        ///über und Funktionalität für den aktuellen Testlauf bietet, oder legt diesen fest.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional attributes for testing
        /// <summary>
        /// Initializes the required attributes for the test methods of this test class.
        /// </summary>
        /// <param name="testContext"></param>
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            System.Windows.Forms.Form form = new System.Windows.Forms.Form();
            form.Visible = false;
            form.ShowInTaskbar = false;
            PresentationParameters param = new PresentationParameters();
            param.DeviceWindowHandle = form.Handle;
            param.IsFullScreen = false;
            GraphicsDevice device = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.HiDef, param);

            Texture2D explosionTex;
            string filePath1 = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) + "\\Out\\explosion.png";
            System.IO.FileStream fs1 = new System.IO.FileStream(filePath1, System.IO.FileMode.Open);
            explosionTex = Texture2D.FromStream(device, fs1);

            SoundEffect explosionSound;
            string filePath2 = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) + "\\Out\\explosionSound.wav";
            System.IO.FileStream fs2 = new System.IO.FileStream(filePath2, System.IO.FileMode.Open);
            explosionSound = SoundEffect.FromStream(fs2);


            TimeSpan totalGameTime = new TimeSpan(0, 0, 0, 0, 5000);
            TimeSpan elapsedGameTime = new TimeSpan(0, 0, 0, 0, 100);
            GameTime gameTime = new GameTime(totalGameTime, elapsedGameTime);

            GlobalGameValues.Values.content.Textures.Clear();
            GlobalGameValues.Values.content.GameSounds.Clear();
            GlobalGameValues.Values.graphicsDevice = device;
            GlobalGameValues.Values.content.addTexture("normalExplosion", explosionTex);
            GlobalGameValues.Values.content.addSound("explosionSound", explosionSound);
            GlobalGameValues.Values.gameTime = gameTime;

            fs1.Close();
            fs2.Close();

        }

        #endregion



        /// <summary>
        ///Test for "isInBoundings"
        ///</summary>
        [TestMethod()]
        public void isInBoundingsTest()
        {
            Bounding test_bounding = new Bounding(new Vector2(10, 10), 50, 50, true);

            Vector3 startPos = new Vector3(1, 2, 3);
            Vector3 objectDirection = Vector3.Zero;
            float birthTime = 1000F;
            BasicExplosion playerExplosion;
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            
            Assert.IsFalse(playerExplosion.isInBoundings());

            //Check bottom boundings
            startPos = new Vector3(15, 3, 59);
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            Assert.IsTrue(playerExplosion.isInBoundings());

            startPos = new Vector3(15, 3, 60);
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            Assert.IsTrue(playerExplosion.isInBoundings());

            startPos = new Vector3(15, 3, 61);
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            Assert.IsFalse(playerExplosion.isInBoundings());

            //Check upper boundings
            startPos = new Vector3(15, 3, 11);
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            Assert.IsTrue(playerExplosion.isInBoundings());

            startPos = new Vector3(15, 3, 10);
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            Assert.IsTrue(playerExplosion.isInBoundings());

            startPos = new Vector3(15, 3, 9);
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            Assert.IsFalse(playerExplosion.isInBoundings());

            //Check right boundings
            startPos = new Vector3(59, 3, 50);
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            Assert.IsTrue(playerExplosion.isInBoundings());

            startPos = new Vector3(60, 3, 50);
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            Assert.IsTrue(playerExplosion.isInBoundings());

            startPos = new Vector3(61, 3, 50);
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            Assert.IsFalse(playerExplosion.isInBoundings());

            //Check left boundings
            startPos = new Vector3(11, 3, 50);
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            Assert.IsTrue(playerExplosion.isInBoundings());

            startPos = new Vector3(10, 3, 50);
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            Assert.IsTrue(playerExplosion.isInBoundings());

            startPos = new Vector3(9, 3, 50);
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);
            playerExplosion.worldBounding = test_bounding;
            Assert.IsFalse(playerExplosion.isInBoundings());



        }

        /// <summary>
        ///Test for "getGamePosition"
        ///</summary>
        [TestMethod()]
        public void getGamePositionTest()
        {
            Vector3 startPos = new Vector3(1, 2, 3);
            Vector3 objectDirection = Vector3.Zero;
            float birthTime = 1000F;
            BasicExplosion playerExplosion;
            playerExplosion = BasicExplosion.CreateExpl_Player(startPos, objectDirection, birthTime);

            Vector2 gamePosition = playerExplosion.getGamePosition();

            Assert.AreEqual(new Vector2(1, 3), gamePosition);
        }
    }
}
